<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="任务名称" prop="taskName"
                :rules="[
                 ]">
              <el-input v-model="inputForm.taskName" placeholder="请填写任务名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="任务" prop="assignmentIds" :rules="[
             ]">
            <el-select v-model="merberRuleList2" multiple placeholder="请选择任务" size="small" style="width: 100%;">
              <el-option v-for="item in merberRuleList"  :key="item.id" :label="item.title"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
            <el-form-item label="任务ids" prop="assignmentIds"
                :rules="[
                  {required: true, message:'任务ids不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.assignmentIds" placeholder="请填写任务ids"     ></el-input>
           </el-form-item>
        </el-col> -->
        <el-col :span="12">
            <el-form-item label="目标数量" prop="taskTargetQuantity"
                :rules="[
                  
                 ]">
              <el-input v-model="inputForm.taskTargetQuantity" placeholder="请填写目标数量"></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        merberRuleList:[],
        merberRuleList2:[],
        inputForm: {
          id: '',
          taskName: '',
          assignmentIds: '',
          taskTargetQuantity: '',
          remarks: '',
          deleteFlag: '0'
        }
      }
    },
    components: {
    
    },
    activated() {
      this.getMethod()
    },
    computed:{
      quantile(){
        let sum = 0
        this.merberRuleList2.forEach(v => {
          this.merberRuleList.forEach(j=>{
            if(v == j.id){
              sum += j.integral
            }
          })
        });
        return sum
      }
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建活动进度信息`
        } else if (method === 'edit') {
          this.title = '修改活动进度信息'
        } else if (method === 'view') {
          this.title = '查看活动进度信息'
        }
      this.merberRuleList2 = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/commonactivityprogress/commonActivityProgress/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.commonActivityProgress)
              if (this.inputForm.assignmentIds) {
                this.merberRuleList2 = this.inputForm.assignmentIds.split(",");
              }
              this.loading = false
            })
          }
        })
      },
      getMethod(){
        this.$http({
          url: '/tmerberrule/tMerberRule/list',
          method: 'get',
          params: {}
        }).then(({data}) => {
          if (data && data.success) {
            this.merberRuleList = data.page.list
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            if (this.merberRuleList2) {
                this.inputForm.assignmentIds = this.merberRuleList2.join(",");
            }
            // this.inputForm.taskTargetQuantity = this.quantile
            this.loading = true
            this.$http({
              url: `/commonactivityprogress/commonActivityProgress/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>